package com.linkhouse.service.impl;

import com.linkhouse.domain.po.TbRentOrder;
import com.linkhouse.domain.vo.TbRentOrderVO;
import com.linkhouse.mapper.QueryOrederMapper;
import com.linkhouse.service.QueryOrederService;
import com.linkhouse.util.IdWorker;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class QueryOrederServiceImpl implements QueryOrederService {

    @Autowired
    private QueryOrederMapper queryOrederMapper;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void queryOreder() {

        Date dNow = new Date(); //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.DATE, -3); //设置为前3月
        dBefore = calendar.getTime(); //得到前3月的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        System.out.println("前3个天的时间是：" + defaultStartDate);
        System.out.println("生成的时间是：" + defaultEndDate);

        List<TbRentOrderVO> list = queryOrederMapper.selectDate(defaultStartDate,defaultEndDate);
        for (TbRentOrderVO vo : list) {
            System.out.println(vo.getPhone());
            rocketMQTemplate.convertAndSend("Phone",vo.getPhone());
        }
    }


    @Override
    public void queryOreder2() {
        Date dNow = new Date(); //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.DATE, +1); //设置为后一天
        dBefore = calendar.getTime(); //得到前3月的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        System.out.println("前3个天的时间是：" + defaultStartDate);
        System.out.println("生成的时间是：" + defaultEndDate);

        List<TbRentOrderVO> list = queryOrederMapper.selectDate2(defaultStartDate,defaultEndDate);
        for (TbRentOrderVO vo : list) {
            System.out.println(vo.getPhone());
            rocketMQTemplate.convertAndSend("Phone",vo.getPhone());
        }
    }


    @Override
    public void insert(Long areaId, Double price,Integer month,Double deposit) {

        IdWorker idWorker = new IdWorker();
        // id
        long id = idWorker.nextId();
        //订单号
        long o = idWorker.nextId();
        String order = String.valueOf(o);




        //创建订单时间
        Date dNow = new Date(); //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.MONTH, + month); //设置为后一天
        dBefore = calendar.getTime(); //得到前3月的时间
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String defaultStart = sdf.format(dBefore); //格式化前  月的时间
        String defaultEnd = sdf.format(dNow); //格式化当前时间


        TbRentOrder rentOrder =null;

        try {
            SimpleDateFormat s =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
            Date defaultStartDate = s.parse(defaultStart);
            Date defaultEndDate = s.parse(defaultEnd);
            System.out.println("结束的时间是：" + defaultStartDate);
            System.out.println("生成的时间是：" + defaultEndDate);
            rentOrder = new TbRentOrder();
            rentOrder.setId(id);
            rentOrder.setPrice(price);
            rentOrder.setAreaId(areaId);
            rentOrder.setOrderId("zfdd" + order);
            rentOrder.setCreateTime(defaultEndDate);
            rentOrder.setExpirationTime(defaultStartDate);
            rentOrder.setDeposit(deposit);
        } catch (ParseException e) {
            e.printStackTrace();

        }

        //增加订单
        queryOrederMapper.insert(rentOrder);
    }

    /**
     * 支付宝回调增加订单流水号
     * @param out_trade_no
     * @param trade_no
     */
    @Override
    public void updateTradeNumByOrderNum(String out_trade_no, String trade_no) {
        queryOrederMapper.updateTradeNumByOrderNum(out_trade_no,trade_no);
    }


    @Override
    public void updateStatusByOrderNum(String out_trade_no) {
        queryOrederMapper.updateStatusByOrderNum(out_trade_no);
    }
}
